package org.bikelab.se3.job;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.bikelab.se3.rdf.reader.RdfReaderFactory;
import org.bikelab.se3.rdf.writer.RdfWriterFactory;
import org.bikelab.se3.util.DirExplorer;
import org.bikelab.se3.util.DirExplorer.Action;
import org.bikelab.se3.util.FileCompressUtil;
import org.semanticweb.yars.nx.Node;

public class Trig2NqJob extends Configured implements Tool {

	public static void main(String[] args) throws Exception {
		int res = ToolRunner.run(new Configuration(), new Trig2NqJob(), args);
		System.exit(res);
	}
	
	@Override
	public int run(String[] arg0) throws Exception {
		final String inputDir = arg0[0];
		final String outputDir = arg0[1];
		
		DirExplorer dExp = new DirExplorer();
		dExp.explore(inputDir, new Action() {
			@Override
			public void perform(File f) {
//				String outputFile = outputDir + "/" + f.getName().replace(".trig", "") + ".nq";
				File outputFile = new File(outputDir + "/" + f.getName().replace(".trig", "") + ".nq");
				try {
//					System.out.println("Converting ... " + f.getPath());
					
					FileWriter writer = new FileWriter(outputFile);
					
					Iterator<Node[]> i = RdfReaderFactory.createTrigReaderForStream(new FileInputStream(f));
					while(i.hasNext()) {
						String nq = RdfWriterFactory.writeQuadInStringWithSPOS(i.next());
//						System.out.println(nq);
						writer.write(nq + "\n");
					}
					writer.close();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					outputFile.delete();
					System.out.println(f.getName());
				}
				System.gc();
			}
		});
		return 0;
	}
}
